<!DOCTYPE html>
<html>
<head>
    <title>客户端上线日志</title>
    {% include 'system/common/header.html' %}
</head>
<body class="pear-container">
    <!-- 搜索框 -->
    <div class="layui-card">
        <div class="layui-card-body">
            <form class="layui-form" action="">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">客户端ID</label>
                        <div class="layui-input-inline">
                            <input type="text" name="clientId" placeholder="请输入客户端ID" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-inline">
                        <button class="pear-btn pear-btn-md pear-btn-primary" lay-submit lay-filter="log-query">
                            <i class="layui-icon layui-icon-search"></i>查询
                        </button>
                        <button type="reset" class="pear-btn pear-btn-md">
                            <i class="layui-icon layui-icon-refresh"></i>重置
                        </button>
                    </div>
                </div>
            </form>
        </div>
    </div>

    <!-- 数据表格 -->
    <div class="layui-card">
        <div class="layui-card-body">
            <table id="log-table" lay-filter="log-table"></table>
        </div>
    </div>

    <!-- 表格工具栏 -->
    {% if authorize("system:iptvadmin:client") %}
    <script type="text/html" id="log-toolbar">
        <button class="pear-btn pear-btn-danger pear-btn-sm" lay-event="clearAll">
            <i class="layui-icon layui-icon-delete"></i>
            全部删除
        </button>
        <button class="pear-btn pear-btn-danger pear-btn-sm" lay-event="clear30">
            <i class="layui-icon layui-icon-delete"></i>
            清除30天前日志
        </button>
        <button class="pear-btn pear-btn-danger pear-btn-sm" lay-event="clear90">
            <i class="layui-icon layui-icon-delete"></i>
            清除90天前日志
        </button>
    </script>
    {% endif %}

    {% include 'system/common/footer.html' %}
    <script>
        layui.use(['table', 'form', 'jquery'], function() {
            let table = layui.table;
            let form = layui.form;
            let $ = layui.jquery;

            let MODULE_PATH = "/iptvadmin/client/log/";

            // 渲染表格
            table.render({
                elem: '#log-table',
                url: MODULE_PATH + 'data',
                page: true,
                cols: [[
                    {field: 'id', title: 'ID', sort: true, width: 80},
                    {field: 'clientId', title: '客户端ID', width: 180},
                    {field: 'clientVersion', title: '客户端版本', width: 120},
                    {field: 'clientIp', title: '客户端IP', width: 130},
                    {field: 'clientModel', title: '设备型号', width: 150},
                    {field: 'clientSystem', title: '系统版本', width: 150},
                    {field: 'lastLogin', title: '最后登录时间', width: 180},
                    {field: 'createTime', title: '创建时间', width: 180}
                ]],
                skin: 'line',
                toolbar: '#log-toolbar',
                defaultToolbar: [{
                    layEvent: 'refresh',
                    title: '刷新',
                    icon: 'layui-icon-refresh',
                }, 'filter', 'print', 'exports']
            });

            // 查询按钮点击事件
            form.on('submit(log-query)', function(data) {
                table.reload('log-table', {
                    where: data.field,
                    page: {curr: 1}
                });
                return false;
            });

            // 修改工具栏事件
            table.on('toolbar(log-table)', function(obj) {
                if (obj.event === 'clearAll') {
                    window.clearAllLogs();
                } else if (obj.event === 'clear30') {
                    window.clearLogs(30);
                } else if (obj.event === 'clear90') {
                    window.clearLogs(90);
                }
            });

             // 清除日志
            window.clearLogs = function(days) {
                layer.confirm('确定要清除' + days + '天前的日志吗？', {
                    icon: 3,
                    title: '提示'
                }, function(index) {
                    layer.close(index);
                    let loading = layer.load();
                    
                    // 修改为正确的请求方式和数据格式
                    $.ajax({
                        url: MODULE_PATH + "clear",
                        data: {days: days},
                        dataType: 'json',
                        type: 'post',
                        success: function(result) {
                            layer.close(loading);
                            if (result.success) {  // 修改为与后端返回格式一致
                                layer.msg(result.msg, {
                                    icon: 1,
                                    time: 1000
                                }, function() {
                                    table.reload('log-table');  // 刷新表格
                                });
                            } else {
                                layer.msg(result.msg, {
                                    icon: 2,
                                    time: 1000
                                });
                            }
                        },
                        error: function() {
                            layer.close(loading);
                            layer.msg('服务器错误', {
                                icon: 2,
                                time: 1000
                            });
                        }
                    });
                });
            }
            // 添加清除全部日志方法
            window.clearAllLogs = function() {
                layer.confirm('确定要清除全部日志吗？此操作不可恢复！', {
                    icon: 3,
                    title: '警告'
                }, function(index) {
                    layer.close(index);
                    let loading = layer.load();
                    
                    $.ajax({
                        url: MODULE_PATH + "clear-all",
                        dataType: 'json',
                        type: 'post',
                        success: function(result) {
                            layer.close(loading);
                            if (result.success) {
                                layer.msg(result.msg, {
                                    icon: 1,
                                    time: 1000
                                }, function() {
                                    table.reload('log-table');
                                });
                            } else {
                                layer.msg(result.msg, {
                                    icon: 2,
                                    time: 1000
                                });
                            }
                        },
                        error: function() {
                            layer.close(loading);
                            layer.msg('服务器错误', {
                                icon: 2,
                                time: 1000
                            });
                        }
                    });
                });
            }

            // 刷新表格
            window.refresh = function() {
                table.reload('log-table');
            }
        });
    </script>
</body>
</html>